package com.thoughtworks.preuniversity.biblioteca;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by padmavb on 6/12/14.
 */

//Job: Direct all the remaining classes
public  class Application {



    public  List<Object> menuItems=new ArrayList<Object>();
    private boolean varToContinue=true;
    private boolean loginUser=false;

    public  static void main(String [] args) throws IOException {


        Application application=new Application();
        Library library=new Library();

        UserInterface userInterface=new UserInterface();
        userInterface.displayWelcome("WELCOME TO BIBLIOTECA\n");

        while (application.varToContinue){

            if(application.loginUser&&application.menuItems.size()!=9)
                application.afterLogin(application);
            else if(application.loginUser==false&&application.menuItems.size()==0)
                application.beforeLogin(application);

            Menu menu=new Menu(application.menuItems);

            menu.displayAvailableMenuItems();

            userInterface.displayMessage("Enter your option");

            int userChoice=userInterface.readUserChoice();

            if(menu.validateInputOption(userChoice)){

                 Object ob= menu.getCorrespondingObject(userChoice);
                ((MenuOperation) ob).doCorrespondingOperation(library);

            }
            else
                userInterface.displayMessage("Enter a valid input");
        }


    }

    public void afterLogin(Application application){

        menuItems.add(new ListBooksMovies("List Books"));
        menuItems.add(new ListBooksMovies("List Movies"));
        menuItems.add(new UserInformation());
        menuItems.add(new LogOut("Log Out", application));
        menuItems.add(new CheckOutBookMovie("Check Out Book"));
        menuItems.add(new CheckOutBookMovie("Check Out Movie"));
        menuItems.add(new ReturnBookMovie("Return Book"));
        menuItems.add(new ReturnBookMovie("Return Movie"));
        menuItems.add(new Quit());
    }
    public void beforeLogin(Application application){

        menuItems.add(new ListBooksMovies("List Books"));
        menuItems.add(new ListBooksMovies("List Movies"));
        menuItems.add(new Login(application));
        menuItems.add(new Quit());
    }

    public void loginOrNot() {

        loginUser=!loginUser;
        menuItems.clear();
    }

    private class Quit extends MenuOperation{

        @Override
        void doCorrespondingOperation(Library library) {

            varToContinue=false;
        }

        @Override
        String giveJobOfClass() {
            return "Quit";
        }

    }

}
